/**
  @Author: 王炜皓
  @Date: 2023-06-07 18:14:19
  @LastEditTime: 2023-06-15 14:09:21
  @LastEditors: 王炜皓
  @Description: 圆绘制类
  
  @FilePath: \cesium-tool\src\views\test\extend\Draw\Circle\CreateCircle.js
 **/

import { countdownEmits } from "element-plus";
import {
  newSessionid,
  getCatesian3FromPX,
  hexToCesiumColor,
} from "../../plotCommon";
const Cesium = window.Cesium;
const CreateCircle = (viewer, handler, resultList, options, callback) => {
  var circle_center_entity = null; // 圆心点 entity
  var temporary_circle_entity = null; // 临时圆形entity
  var circle_entity = null; // 结果圆形entity
  var circle_end_point = null; // 结束点
  var circle_center_point = null; // 圆心点
  var viewer = viewer;
  var style=options.style||Cesium.Color.White
  var instyle=options.instyle||Cesium.Color.White
  var alfa=1-options.alfa||1
  const ground=options.ground||false
  const code=options.code||newSessionid();
  if (circle_entity !== null) {
    viewer.entities.remove(circle_center_entity);
    viewer.entities.remove(temporary_circle_entity);
    viewer.entities.remove(circle_entity);
    circle_center_entity = null;
    temporary_circle_entity = null;
    circle_entity = null;
    circle_end_point = null;
    circle_center_point = null;
  }

  // 清除所有点击事件
  if (handler) {
    handler.removeInputAction(Cesium.ScreenSpaceEventType.LEFT_CLICK);
    handler.removeInputAction(Cesium.ScreenSpaceEventType.MOUSE_MOVE);
  }

  // 鼠标点击左键
  handler.setInputAction((event) => {
    // 屏幕坐标转为世界坐标
    let cartesian = viewer.scene.globe.pick(
      viewer.camera.getPickRay(event.position),
      viewer.scene
    );
    let ellipsoid = viewer.scene.globe.ellipsoid;
    let cartographic = ellipsoid.cartesianToCartographic(cartesian);
    let lon = Cesium.Math.toDegrees(cartographic.longitude); // 经度
    let lat = Cesium.Math.toDegrees(cartographic.latitude); // 纬度

    // 判断圆心是否已经绘制，如果绘制了，再次点击左键的时候，就是绘制最终结果圆形
    if (circle_center_entity) {
      // 设置最终点
      circle_end_point = {
        lon: lon,
        lat: lat,
        height: 0,
      };
      // 绘制结果多边形
      // draw_circle();
      circle_entity = viewer.entities.add({
		code:code,
		name: "Circle",
        position: Cesium.Cartesian3.fromDegrees(
          circle_center_point.lon,
          circle_center_point.lat
        ),
        ellipse: {
          // 半短轴（画圆：半短轴和半长轴一致即可）
          semiMinorAxis: two_points_distance(
            circle_center_point,
            circle_end_point
          ),
          // 半长轴
          semiMajorAxis: two_points_distance(
            circle_center_point,
            circle_end_point
          ),
          // 填充色
          material: hexToCesiumColor(style,alfa),
          // 是否有边框
          outline: true,
          // 边框颜色
          outlineColor: hexToCesiumColor(instyle,alfa),
          // 边框宽度
          outlineWidth: 4,
        },
      });
      // 清除事件
      handler.removeInputAction(Cesium.ScreenSpaceEventType.LEFT_CLICK);
      handler.removeInputAction(Cesium.ScreenSpaceEventType.MOUSE_MOVE);
      // 清除 绘制的中心点和临时圆
      viewer.entities.remove(circle_center_entity);
      viewer.entities.remove(temporary_circle_entity);
    } else {
      // 设置中心点坐标和结束点坐标
      circle_end_point = circle_center_point = {
        lon: lon,
        lat: lat,
        height: 0,
      };
      // 绘制圆心点
      // create_circle_center_point([lon, lat]);
      circle_center_entity = viewer.entities.add({
		code:code,

		name: "Circle",
        // fromDegrees（经度，纬度，高度）以度为单位的经度和纬度值返回Cartesian3位置
        position: Cesium.Cartesian3.fromDegrees(lon, lat, 100),
        point: {
          // 点的大小（像素）
          pixelSize: 5,
          // 点位颜色，fromCssColorString 可以直接使用CSS颜色
          color: hexToCesiumColor(style,alfa),
          // 边框颜色
          outlineColor: hexToCesiumColor(instyle,alfa),
          // 边框宽度(像素)
          outlineWidth: 2,
          // 是否显示
          show: true,
        },
      });
      // 开始绘制动态圆形
      // draw_dynamic_circle(circle_center_point);
      let point = circle_center_point;
      temporary_circle_entity = viewer.entities.add({
		code:code,
		name: "Circle",
        position: Cesium.Cartesian3.fromDegrees(point.lon, point.lat),
        ellipse: {
          // 半短轴（画圆：半短轴和半长轴一致即可）
          semiMinorAxis: new Cesium.CallbackProperty(() => {
            // PolygonHierarchy 定义多边形及其孔的线性环的层次结构（空间坐标数组）
            return two_points_distance(point, circle_end_point);
          }, false),
          // 半长轴
          semiMajorAxis: new Cesium.CallbackProperty(() => {
            // PolygonHierarchy 定义多边形及其孔的线性环的层次结构（空间坐标数组）
            return two_points_distance(point, circle_end_point);
          }, false),
          // 填充色
          material: hexToCesiumColor(style,alfa),
          // 是否有边框
          outline: true,
          // 边框颜色
          outlineColor:hexToCesiumColor(instyle,1),
          // 边框宽度
          outlineWidth: 4,
        },
      });
      // 鼠标移动--实时绘制圆形
      handler.setInputAction((event) => {
        // 屏幕坐标转为世界坐标
        let cartesian = viewer.scene.globe.pick(
          viewer.camera.getPickRay(event.endPosition),
          viewer.scene
        );
        let ellipsoid = viewer.scene.globe.ellipsoid;
        let cartographic = ellipsoid.cartesianToCartographic(cartesian);
        let lon = Cesium.Math.toDegrees(cartographic.longitude); // 经度
        let lat = Cesium.Math.toDegrees(cartographic.latitude); // 纬度

        if (temporary_circle_entity) {
          // 修改结束点-用于动态绘制圆形
          circle_end_point = {
            lon: lon,
            lat: lat,
            height: 0,
          };
        }
      }, Cesium.ScreenSpaceEventType.MOUSE_MOVE);
    }
  }, Cesium.ScreenSpaceEventType.LEFT_CLICK);
};

// 根据经纬度计算两点之前的直线距离
function two_points_distance(start_point, end_point) {
  // 经纬度转换为世界坐标
  var start_position = Cesium.Cartesian3.fromDegrees(
    start_point.lon,
    start_point.lat,
    start_point.height
  );
  var end_position = Cesium.Cartesian3.fromDegrees(
    end_point.lon,
    end_point.lat,
    end_point.height
  );
  // 返回两个坐标的距离（单位：米）
  return Cesium.Cartesian3.distance(start_position, end_position);
}
export default CreateCircle;
